package com.hengxinguotong.zhihuichengjian.ui.device;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceSelectedAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Device;
import com.hengxinguotong.zhihuichengjian.bean.DeviceListRes;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/10.
 */

public class DeviceSelectedActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.device_lv)
    PullToRefreshListView deviceLv;

    private int pageIndex = 1;
    private List<Device> devices;
    private DeviceSelectedAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list);
        ButterKnife.bind(this);
        deviceLv.setMode(PullToRefreshBase.Mode.BOTH);
        deviceLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                getDeviceList(null);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getDeviceList(null);
            }
        });
        deviceLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (adapter != null) {
                    adapter.setSelect(position - 1);
                }
            }
        });
        getDeviceList(getResources().getString(R.string.loading));
    }

    private void getDeviceList(String alert){
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this,"constructionid"));
            jsonObject.put("pageNum", pageIndex);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/equipmentRegister/queryAllList/" + SPUtil.getString(this,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (deviceLv.isRefreshing()) {
                    deviceLv.onRefreshComplete();
                }

                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    DeviceListRes deviceListRes = gson.fromJson(json, DeviceListRes.class);
                    addData(deviceListRes.getValue());
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void addData(List<Device> datas) {
        if (pageIndex == 1) {
            if(datas.size()==0){
                showEmptyView(deviceLv);
            }else{
                dismissEmptyView();
            }
            devices = datas;
            adapter = new DeviceSelectedAdapter(devices, this);
            deviceLv.setAdapter(adapter);
            if (datas.size() == Utils.ONCECOUNT) {
                deviceLv.setMode(PullToRefreshBase.Mode.BOTH);
            }
        } else {
            devices.addAll(datas);
            adapter.notifyDataSetChanged();
        }

        if (datas.size() < Utils.ONCECOUNT) {
            deviceLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        }

        if (datas.size() != 0) {
            pageIndex++;
        }
    }

    @OnClick({R.id.back_tv, R.id.save_tv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_tv:
                finish();
                break;
            case R.id.save_tv:
                if (adapter != null && adapter.getSelect() != null) {
                    Intent intent = getIntent();
                    intent.putExtra("device", adapter.getSelect());
                    setResult(1, intent);
                    finish();
                }else{
                    showToast("至少选择一项");
                }
                break;
        }
    }
}
